package x10.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import x10.enums.Interfaces;
import x10.log.CustomLevel;

/**
 * Class for managing application properties
 * @author Denny Sheirer
 * @date   2011
 */
public class PropertiesManager {
    Logger logger = Logger.getLogger("x10.properties.PropertiesManager");
    private Properties properties = new Properties();
    

    public PropertiesManager(){
    }
    
    public PropertiesManager(String filename){
	this();
	load(filename);
    }

    public void set(String key, String value){
        properties.setProperty(key, value);
    }

    public Interfaces getX10Interface(String key, Interfaces defaultInterface){
	if(properties.contains(key)){
	    String x10Interface = getString(key, defaultInterface.toString());
	    return Interfaces.valueOf(x10Interface);
	} else {
	    logger.fine("Couldn't find " + key + " using default: " + defaultInterface.toString());
	    return defaultInterface;
	}
    }
    
    public String getString(String key, String defaultValue){
        if(properties.contains(key)){
            return properties.getProperty(key);
        } else {
	    logger.fine("Couldn't find " + key + " using default: " + defaultValue);
            return defaultValue;
        }
    }

    public Level getLogLevel(String key, Level defaultLevel){
        if(properties.contains(key)){
            return CustomLevel.parse(properties.getProperty(key));
        } else {
	    logger.fine("Couldn't find " + key + " using default: " + defaultLevel.toString());
            return defaultLevel;
        }
    }

    public boolean getBoolean(String key, boolean defaultValue){
	boolean returnValue = defaultValue;
	if(properties.contains(key)){
            String temp = properties.getProperty(key);
            if(temp.contentEquals("true")){
        	returnValue = true;
            } else if (temp.contentEquals("false")){
        	returnValue = false;
            }
        } else {
	    logger.fine("Couldn't find " + key + " using default: " + defaultValue);
        }
	return returnValue;
    }

    public int getInt(String key, int defaultValue){
	int returnValue = defaultValue;
	if(properties.contains(key)){
	    try{
		returnValue = Integer.parseInt(properties.getProperty(key));
	    } catch (Exception e){
		System.out.println("Error parsing property: " + key + " and casting to an integer");
	    }
        } else {
	    logger.fine("Couldn't find " + key + " using default: " + String.valueOf(defaultValue));
        }
        return returnValue;
    }

    private void load(String filename){
        try{
            FileInputStream in = new FileInputStream(new File(filename));
            properties.load(in);
            in.close();
        } catch (FileNotFoundException fnfe){
            System.out.println("Properties Manager(load()) - File Not Found Exception");
        } catch (IOException ioe){
            System.out.println("Properties Manager(load()) - IO Exception, could not read the server.properties file");
        }
    }
}
